package kr.saeildadim.androidexam_saeildamil.chapter12;

import java.util.ArrayList;

import kr.saeildadim.androidexam_saeildamil.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class Ch12_ListAddDelMulti extends Activity {
	ArrayList<String> arList;
	ArrayAdapter<String> adapter;
	ListView list;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch12_listadddel);
		arList= new ArrayList<String>();
		arList.add("First");
		arList.add("Second");
		arList.add("Third");
		arList.add("Fourth");
		arList.add("Fifth");
		
		adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, arList);
		
		list= (ListView) findViewById(R.id.ch12_listadddel_list);
		list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		list.setAdapter(adapter);
		list.setOnItemClickListener(listener);
	
	}
	AdapterView.OnItemClickListener listener= new OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
				Toast.makeText(Ch12_ListAddDelMulti.this, arList.get(position), Toast.LENGTH_LONG).show();
			
		}
	};
	
	public void mOnClick(View v){
		EditText et = (EditText) findViewById(R.id.ch12_listadddel_edittext);
		switch(v.getId()){
		case R.id.ch12_listadddel_addbnt:
			String str = et.getText().toString();
			et.setText("");
			arList.add(str);
			adapter.notifyDataSetChanged();
			break;
		case R.id.ch12_listadddel_delbnt:
			SparseBooleanArray sbar = list.getCheckedItemPositions();
			Toast.makeText(this, "선택한사이즈="+sbar.size(), Toast.LENGTH_LONG).show();
			if(sbar.size()!=0){
			for(int i=list.getCount();i>0;i--){
				Toast.makeText(this, ""+sbar.get(i), Toast.LENGTH_LONG).show();
				if(sbar.get(i)){
					arList.remove(i);
					}
				}
			}
			list.clearChoices();
			adapter.notifyDataSetChanged();
			break;
		}
	}
}
